﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ConsolidationTool.Common.Entities;
using System.Collections.Generic;
using ConsolidationTool.Common;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ConsolidationTool.WebParts.ControlTemplates;

namespace ConsolidationTool.WebParts.Flash_Input.Flsha_Input_HR.HRNewForm
{
    public partial class HRNewFormUserControl : FSUserControlBase
    {
        List<Group> regionGroup = new List<Group>();
        List<Group> subRegionGroup = new List<Group>();
        List<Group> entityGroup = new List<Group>();
        public bool isFirstMonth;

        protected void Page_Load(object sender, EventArgs e)
        {
           TopSelector= (commonEntitySelector as CommonEntitySelector);
           TopSelector.EntitySelectorChanged += new CommonEntitySelector.EntitySelectorChangeHandler(TopSelector_EntitySelectorChanged);
           (commonButtomAction as CommonButtomAction).SaveButtonClicked += new CommonButtomAction.SaveButtonClickHandler(ESNewFormUserControl_SaveButtonClicked);
           isFirstMonth = TopSelector.monthTextBox.Text.Trim() == "1";

            if (!IsPostBack)
            {
                
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!IsPostBack)
                base.FormInitialization();
            base.OnPreRender(e);
        }

        public override SPListItem Item
        {
            get
            {
                return ConsolidationTool_CoName.ListItem;
            }
        }

        void ESNewFormUserControl_SaveButtonClicked(object sender, EventArgs e)
        {
            this.Page.Validate();
            base.Save();
        }

        void TopSelector_EntitySelectorChanged(object sender, int EntityID)
        {
 
            //邦定上个月数据
            BindLastMonth(TopSelector.lbCoCd.Text, TopSelector.yearTextBox.Text, TopSelector.monthTextBox.Text);
            //邦定从一月份到当前月份的上一个月的总和
            BindYTDLastMonth(TopSelector.lbCoCd.Text, TopSelector.yearTextBox.Text, TopSelector.monthTextBox.Text);
            BindLastDecData(TopSelector.lbCoCd.Text, TopSelector.yearTextBox.Text); 
        }
       
       
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (TopSelector.drpEntity.SelectedIndex == 0)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "jsNote", "<script>alert('Entity is null');</script>");
                return;
            }
            this.Page.Validate();
            if (Page.IsValid)
            {
                SPListItem item = ConsolidationTool_CoCd.ListItem;

                item["CoCd"] = TopSelector.lbCoCd.Text;
                item["CoFullName"] = TopSelector.lbCompanyFullName.Text;
                item["CoName"] = TopSelector.drpEntity.SelectedItem.Text;
                item["Year"] = TopSelector.yearTextBox.Text;
                item["Month"] = TopSelector.monthTextBox.Text ;
                item["Region"] = TopSelector.drpRegion.SelectedItem.Text;
                item["SubRegion"] = TopSelector.drpSubRegion.SelectedItem.Text;

                Title.ItemFieldValue = TopSelector.lbCoCd.Text + "_" + TopSelector.yearTextBox.Text + TopSelector.monthTextBox.Text;
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                Utilities.CloseForm();
            }
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {
            Utilities.CloseForm();
        }
        /// <summary>
        /// 邦定上一个月的数据
        /// </summary>
        void BindLastMonth(string coCd,string year,string month)
        {
            string y = year;
            string m =(Convert.ToInt32( month)-1).ToString();
            if (Convert.ToInt32(month) == 1)
            {
                y = (Convert.ToInt32(year) - 1).ToString();
                m = "12";
            }
            SPListItem it = Common.BusinessService.GetItem(coCd, y, m, "HR");
            if (it != null)
            {
                Label_HR01010100.Text = Common.Utilities.GetItemValue(it, "HR01010100");
                Label_HR01010101.Text = Common.Utilities.GetItemValue(it, "HR01010101");
                Label_HR01010102.Text = Common.Utilities.GetItemValue(it, "HR01010102");
                Label_HR01010103.Text = Common.Utilities.GetItemValue(it, "HR01010103");
                Label_HR01010104.Text = Common.Utilities.GetItemValue(it, "HR01010104");
                Label_HR01010105.Text = Common.Utilities.GetItemValue(it, "HR01010105");
                Label_HR01010106.Text = Common.Utilities.GetItemValue(it, "HR01010106");
                Label_HR01010200.Text = Common.Utilities.GetItemValue(it, "HR01010200");
                Label_HR01010301.Text = Common.Utilities.GetItemValue(it, "HR01010301");
                Label_HR01010302.Text = Common.Utilities.GetItemValue(it, "HR01010302");
                Label_HR01020100.Text = Common.Utilities.GetItemValue(it, "HR01020100");
                Label_HR01020101.Text = Common.Utilities.GetItemValue(it, "HR01020101");
                Label_HR01020102.Text = Common.Utilities.GetItemValue(it, "HR01020102");
                Label_HR01020103.Text = Common.Utilities.GetItemValue(it, "HR01020103");
                Label_HR01020104.Text = Common.Utilities.GetItemValue(it, "HR01020104");
                Label_HR01020105.Text = Common.Utilities.GetItemValue(it, "HR01020105");
                Label_HR01020106.Text = Common.Utilities.GetItemValue(it, "HR01020106");
                Label_HR01020200.Text = Common.Utilities.GetItemValue(it, "HR01020200");
                Label_HR01030100.Text = Common.Utilities.GetItemValue(it, "HR01030100");
                Label_HR01030101.Text = Common.Utilities.GetItemValue(it, "HR01030101");
                Label_HR01030102.Text = Common.Utilities.GetItemValue(it, "HR01030102");
                Label_HR01040100.Text = Common.Utilities.GetItemValue(it, "HR01040100");
                Label_HR01040101.Text = Common.Utilities.GetItemValue(it, "HR01040101");
                Label_HR01040102.Text = Common.Utilities.GetItemValue(it, "HR01040102");
                Label_HR01050100.Text = Common.Utilities.GetItemValue(it, "HR01050100");
                Label_HR01060100.Text = Common.Utilities.GetItemValue(it, "HR01060100");
                Label_HR01060101.Text = Common.Utilities.GetItemValue(it, "HR01060101");
                Label_HR01060102.Text = Common.Utilities.GetItemValue(it, "HR01060102");
                Label_HR01070100.Text = Common.Utilities.GetItemValue(it, "HR01070100");
                Label_HR01070101.Text = Common.Utilities.GetItemValue(it, "HR01070101");
                Label_HR01070102.Text = Common.Utilities.GetItemValue(it, "HR01070102");
                Label_HR01080100.Text = Common.Utilities.GetItemValue(it, "HR01080100");
                Label_HR01090100.Text = Common.Utilities.GetItemValue(it, "HR01090100");
                Label_HR01020300.Text = Common.Utilities.GetItemValue(it, "HR01020300");

                //上一个月的YTD
                if (Convert.ToInt32(month) == 1)
                {
                    HiddenField_HR060100YTD_LastMonth.Value = "0";
                    HiddenField_HR060101YTD_LastMonth.Value = "0";
                    HiddenField_HR060102YTD_LastMonth.Value = "0";
                    FormField_Headcount_Turnover_YTD.Value = "0";
                    FormField_Voluntary_YTD.Value = "0";
                    FormField_Involuntary_YTD.Value = "0";

                    HiddenField_HR01080100YTD_LastMonth.Value = "0";
                    HiddenField_HR01090100YTD_LastMonth.Value = "0";
                    FormField_Traininghours_YTD.Value = "0";
                    FormField_Numberofpromotion_YTD.Value = "0";
                }
                else
                {
                    HiddenField_HR060100YTD_LastMonth.Value = Common.Utilities.GetItemValue(it, "HR01060100_YTD");
                    HiddenField_HR060101YTD_LastMonth.Value = Common.Utilities.GetItemValue(it, "HR01060101_YTD");
                    HiddenField_HR060102YTD_LastMonth.Value = Common.Utilities.GetItemValue(it, "HR01060102_YTD");
                    FormField_Headcount_Turnover_YTD.Value = Common.Utilities.GetItemValue(it, "HR01060100_YTD");
                    FormField_Voluntary_YTD.Value = Common.Utilities.GetItemValue(it, "HR01060101_YTD");
                    FormField_Involuntary_YTD.Value = Common.Utilities.GetItemValue(it, "HR01060102_YTD");

                    HiddenField_HR01080100YTD_LastMonth.Value = Common.Utilities.GetItemValue(it, "HR01080100_YTD");
                    HiddenField_HR01090100YTD_LastMonth.Value = Common.Utilities.GetItemValue(it, "HR01090100_YTD");
                    FormField_Traininghours_YTD.Value = Common.Utilities.GetItemValue(it, "HR01080100_YTD");
                    FormField_Numberofpromotion_YTD.Value = Common.Utilities.GetItemValue(it, "HR01090100_YTD");
                }
                
            }
            else
            {
                Label_HR01010100.Text = "0";
                Label_HR01010101.Text = "0";
                Label_HR01010102.Text = "0";
                Label_HR01010103.Text = "0";
                Label_HR01010104.Text = "0";
                Label_HR01010105.Text = "0";
                Label_HR01010106.Text = "0";
                Label_HR01010200.Text = "0";
                Label_HR01010301.Text = "0";
                Label_HR01010302.Text = "0";
                Label_HR01020100.Text = "0";
                Label_HR01020101.Text = "0";
                Label_HR01020102.Text = "0";
                Label_HR01020103.Text = "0";
                Label_HR01020104.Text = "0";
                Label_HR01020105.Text = "0";
                Label_HR01020106.Text = "0";
                Label_HR01020200.Text = "0";
                Label_HR01030100.Text = "0";
                Label_HR01030101.Text = "0";
                Label_HR01030102.Text = "0";
                Label_HR01040100.Text = "0";
                Label_HR01040101.Text = "0";
                Label_HR01040102.Text = "0";
                Label_HR01050100.Text = "0";
                Label_HR01060100.Text = "0";
                Label_HR01060101.Text = "0";
                Label_HR01060102.Text = "0";
                Label_HR01070100.Text = "0";
                Label_HR01070101.Text = "0";
                Label_HR01070102.Text = "0";
                Label_HR01080100.Text = "0";
                Label_HR01090100.Text = "0";
                Label_HR01020300.Text = "0";

                //上一个月的YTD
                HiddenField_HR060100YTD_LastMonth.Value = "0";
                HiddenField_HR060101YTD_LastMonth.Value = "0";
                HiddenField_HR060102YTD_LastMonth.Value = "0";

                HiddenField_HR01080100YTD_LastMonth.Value = "0";
                HiddenField_HR01090100YTD_LastMonth.Value = "0";
    
                FormField_Headcount_Turnover_YTD.Value = "0";
                FormField_Voluntary_YTD.Value = "0";
                FormField_Involuntary_YTD.Value = "0";

                FormField_Traininghours_YTD.Value = "0";
                FormField_Numberofpromotion_YTD.Value = "0";
            }
        }

        /// <summary>
        /// 邦定YTDLastMonth
        /// </summary>
        void BindYTDLastMonth(string coCd,string year,string month)
        {
            int HR01010100YTD = 0;
    
            int m=Convert.ToInt32(month);
            for (int ii = 1; ii < m; ii++)
            {
                string str_hr100 = "";
                SPListItem it = Common.BusinessService.GetItem(coCd, year, ii+"", "HR");
                if (it != null)
                {
                    str_hr100 = Common.Utilities.GetItemValue(it, "HR01010100");

                    HR01010100YTD += (str_hr100 == "" ? 0 : Convert.ToInt32(str_hr100)); 
                } 
            }
            LabelHR01010100YTD.Text = HR01010100YTD + "";
        }
        /// <summary>
        /// 取去年12月的HeadCount
        /// </summary>
        void BindLastDecData(string coCd,string year)
        {
            string  m = "12";
            string y = (Convert.ToInt32(year) - 1).ToString();
            SPListItem it = Common.BusinessService.GetItem(coCd, y, m, "HR");

            if (it != null)
            {
                string lastDecHeadCount = Common.Utilities.GetItemValue(it, "HR01010100");
                if (!string.IsNullOrEmpty(lastDecHeadCount))
                    HiddenField_LastDecHeadCount.Value = lastDecHeadCount;
                else
                    HiddenField_LastDecHeadCount.Value = "0";
            }
            else
            {
                HiddenField_LastDecHeadCount.Value = "0";
            }
        }
    }
}
